//
//  GoalSetupPickerView.swift
//  DaFit
//
//  Created by 尹琼 on 2022/5/18.
//  Copyright © 2022 sylar. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import RxDataSources
import NSObject_Rx
import MYModule

class GoalSetupPickerView: View {
    
    
    public var titles: [String]? {
        didSet {
            pickerView.reloadAllComponents()
        }
    }
    
    public var didSelectIndex: Int = 0
    
    private var didSelectClosure: ((Int) -> ())?
        
    override func makeUI() {
        super.makeUI()
        
        addSubview(pickerView)
        addSubview(BoxView)
        
        pickerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        BoxView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.leading.equalTo(10)
            make.height.equalTo(40)
        }
    }
    
    public func returnDidSelectClosure(_ closure: @escaping (Int) -> ()) {
        self.didSelectClosure = closure
    }
    
    public func didSelectItem(atIndex index: Int) {
        if self.didSelectIndex == index {
            return
        }
        self.pickerView.selectRow(index, inComponent: 0, animated: true)
    }
    
    //MARK: - lazy load -
    
    private lazy var BoxView: View = {
        let view = View()
        view.layer.cornerRadius = 6
        view.layer.borderColor = UIColor.theme.cgColor
        view.layer.borderWidth = 2.0
        view.isUserInteractionEnabled = false
        return view
    }()
    
    private lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.tintColor = UIColor.assist_D0D0D0
        pickerView.dataSource = self
        pickerView.delegate = self
        return pickerView
    }()
}

extension GoalSetupPickerView: UIPickerViewDataSource, UIPickerViewDelegate {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return titles?.count ?? 0
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
       return 18
    }
    
    
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

        if #available(iOS 14.0, *) {
           pickerView.subviews[1].backgroundColor = .clear
        } else {
            if pickerView.subviews.count >= 3 {
                pickerView.subviews[1].isHidden = true
                pickerView.subviews[2].isHidden = true
            }
        }
        

        guard let titles = titles else { return nil }
        let title = titles[row]

        let attriText = NSAttributedString(string: title, attributes: [.font: Fonts.systemFont(ofSize: 18), .foregroundColor: UIColor.theme])

        return attriText
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.didSelectIndex = row
        if self.didSelectClosure != nil {
            self.didSelectClosure?(row)
        }
    }
    


}

